From b65f717688423c3b3f770d38d63ad681a53121c4 Mon Sep 17 00:00:00 2001 From: "kfraser@localhost.localdomain" Date: Fri, 8 Dec 2006 11:57:06 +0000 Subject: [PATCH] [LIBXC] Clean up use of sterror(). Define a thread-safe version which uses strerror_r(), and tries to cope with the POSIX and GNU versions of that function. Signed-off-by: Keir Fraser --- tools/libxc/ia64/xc_ia64_linux_save.c | 2 +- tools/libxc/xc_core.c | 4 ++-- tools/libxc/xc_linux_build.c | 4 ++-- tools/libxc/xc_private.c | 13 +++++++++++++ tools/libxc/xc_private.h | 3 ++- 5 files changed, 20 insertions(+), 6 deletions(-) diff --git a/tools/libxc/ia64/xc_ia64_linux_save.c b/tools/libxc/ia64/xc_ia64_linux_save.c index 42ec21c802..d98725445a 100644 --- a/tools/libxc/ia64/xc_ia64_linux_save.c +++ b/tools/libxc/ia64/xc_ia64_linux_save.c @@ -353,7 +353,7 @@ xc_linux_save(int xc_handle, int io_fd, uint32_t dom, uint32_t max_iters, It will be remarked dirty. FIXME: to be tracked. */ fprintf(stderr, "cannot map page %lx: %s\n", - page_array[N], strerror (errno)); + page_array[N], safe_strerror(errno)); continue; } diff --git a/tools/libxc/xc_core.c b/tools/libxc/xc_core.c index 550d5691e1..035bf301e5 100644 --- a/tools/libxc/xc_core.c +++ b/tools/libxc/xc_core.c @@ -140,7 +140,7 @@ static int local_file_dump(void *args, char *buffer, unsigned int length) bytes = write(da->fd, &buffer[offset], length-offset); if ( bytes <= 0 ) { - PERROR("Failed to write buffer: %s", strerror(errno)); + PERROR("Failed to write buffer"); return -errno; } } @@ -158,7 +158,7 @@ xc_domain_dumpcore(int xc_handle, if ( (da.fd = open(corename, O_CREAT|O_RDWR, S_IWUSR|S_IRUSR)) < 0 ) { - PERROR("Could not open corefile %s: %s", corename, strerror(errno)); + PERROR("Could not open corefile %s", corename); return -errno; } diff --git a/tools/libxc/xc_linux_build.c b/tools/libxc/xc_linux_build.c index edf95c0fc0..a2b5274083 100644 --- a/tools/libxc/xc_linux_build.c +++ b/tools/libxc/xc_linux_build.c @@ -593,8 +593,8 @@ static int setup_guest(int xc_handle, /* shared_info page starts its life empty. */ shared_info = xc_map_foreign_range( xc_handle, dom, PAGE_SIZE, PROT_READ|PROT_WRITE, shared_info_frame); - printf("shared_info = %p, err=%s frame=%lx\n", - shared_info, strerror (errno), shared_info_frame); + printf("shared_info = %p frame=%lx\n", + shared_info, shared_info_frame); //memset(shared_info, 0, PAGE_SIZE); /* Mask all upcalls... */ for ( i = 0; i < MAX_VIRT_CPUS; i++ ) diff --git a/tools/libxc/xc_private.c b/tools/libxc/xc_private.c index 3ff0b791d0..ed76cf003c 100644 --- a/tools/libxc/xc_private.c +++ b/tools/libxc/xc_private.c @@ -483,6 +483,19 @@ unsigned long xc_make_page_below_4G( return new_mfn; } +char *safe_strerror(int errcode) +{ + static __thread char errbuf[32]; +#ifdef __GLIBC__ + /* Broken GNU definition of strerror_r may not use our supplied buffer. */ + return strerror_r(errcode, errbuf, sizeof(errbuf)); +#else + /* Assume we have the POSIX definition of strerror_r. */ + strerror_r(errcode, errbuf, sizeof(errbuf)); + return errbuf; +#endif +} + /* * Local variables: * mode: C diff --git a/tools/libxc/xc_private.h b/tools/libxc/xc_private.h index f087e7de69..6dfbdead7a 100644 --- a/tools/libxc/xc_private.h +++ b/tools/libxc/xc_private.h @@ -59,11 +59,12 @@ #define PPRINTF(_f, _a...) #endif +char *safe_strerror(int errcode); void xc_set_error(int code, const char *fmt, ...); #define ERROR(_m, _a...) xc_set_error(XC_INTERNAL_ERROR, _m , ## _a ) #define PERROR(_m, _a...) xc_set_error(XC_INTERNAL_ERROR, _m " (%d = %s)", \ - ## _a , errno, strerror(errno)) + ## _a , errno, safe_strerror(errno)) int lock_pages(void *addr, size_t len); void unlock_pages(void *addr, size_t len); -- 2.30.2